package com.ruicar.afs.cloud.afscase.loanapprove.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.loanapprove.entity.CasePriorityRecord;
import com.ruicar.afs.cloud.afscase.loanapprove.mapper.CasePriorityRecordMapper;
import com.ruicar.afs.cloud.afscase.loanapprove.service.CasePriorityRecordService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * 优先级等级评定历史表
 * add by fangchenliang
 */
@Service
public class CasePriorityRecordServiceImpl extends ServiceImpl<CasePriorityRecordMapper, CasePriorityRecord> implements CasePriorityRecordService {
    @Override
    public boolean save(CasePriorityRecord record){
        /**判断最新的优先级是否相等，相等则跳过保存*/
       List<CasePriorityRecord> list =this.list(Wrappers.<CasePriorityRecord>lambdaQuery()
                .eq(StringUtils.isNotBlank(record.getContractNo()),CasePriorityRecord::getContractNo,record.getContractNo())
                .eq(StringUtils.isNotBlank(record.getApplyNo()),CasePriorityRecord::getApplyNo,record.getApplyNo())
                .orderByDesc(CasePriorityRecord::getCreateTime));
       if(CollectionUtils.isNotEmpty(list)){
           CasePriorityRecord hisRecord=list.get(0);
           if(hisRecord!=null&&!record.getPriority().equals(hisRecord.getPriority())){
               return super.save(record);
           }
       }else {
           return super.save(record);
       }
        return true;
    }
}
